package com.szh.taobaounion.ui.fragment;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.google.android.material.tabs.TabLayout;
import com.szh.taobaounion.Presenter.impl.HomePresenterImpl;
import com.szh.taobaounion.R;
import com.szh.taobaounion.base.BaseFragment;
import com.szh.taobaounion.model.domain.CateGories;
import com.szh.taobaounion.ui.activity.IMainActivity;
import com.szh.taobaounion.ui.activity.MainActivity;
import com.szh.taobaounion.ui.activity.ScanQrCodeActivity;
import com.szh.taobaounion.ui.adapter.HomePagerAdapter;
import com.szh.taobaounion.ui.adapter.LooperPagerAdapter;
import com.szh.taobaounion.utils.LogUtils;
import com.szh.taobaounion.utils.PresenterManager;
import com.szh.taobaounion.view.IHomeCallBack;
import com.vondear.rxfeature.activity.ActivityScanerCode;

import butterknife.BindView;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends BaseFragment implements IHomeCallBack {

    private HomePresenterImpl homePresenter;
    @BindView(R.id.home_indicator)
    public TabLayout mTabLayout;
    @BindView(R.id.home_pager)
    public ViewPager homePager;


    @BindView(R.id.home_page_input_box)
    public EditText inputText;

    @BindView(R.id.scan_icon)
    public View scanBtn;
    private HomePagerAdapter mAdapter;



    public HomeFragment() {
        // Required empty public constructor
    }
    @Override
    protected int getRootViewResId() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView(View rootView) {
        //初始化控件
        mTabLayout.setupWithViewPager(homePager);
        //给ViewPager这只适配器
        mAdapter = new HomePagerAdapter(getChildFragmentManager());
        homePager.setAdapter(mAdapter);


    }

    @Override
    protected void initListener() {
        scanBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getContext(), ScanQrCodeActivity.class));
            }
        });
    }

    @Override
    protected View lodeRootView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.base_home_fragment_layout,container,false);
    }

    @Override
    protected void initPresenter() {
        //创建presenter
        homePresenter = PresenterManager.getInstance().getmHomePresenter();
        homePresenter.registerViewCallBack(this);
        inputText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentActivity activity = getActivity();
                if (activity instanceof IMainActivity) {
               ((MainActivity) activity).switch2Search();
                }
            }
        });

    }
    @Override
    protected void LoadData() {
        //加载数据
        homePresenter.getCattgories();
    }

    @Override
    public void onCategoriesLoaded(CateGories cateGories) {
        setUpState(State.SUCCESS);
        LogUtils.d(this,"onCategoriesLoaded---");
        //加载的数据从这里回来
        if (mAdapter != null) {
            //homePager.setOffscreenPageLimit(cateGories.getData().size());
            mAdapter.setCategories(cateGories);
        }


    }

    @Override
    public void onError() {
    setUpState(State.ERROR);
    }

    @Override
    public void onLoading() {
        setUpState(State.LOADING);
    }

    @Override
    public void onEmpty() {
        setUpState(State.Empty);
    }

    @Override
    protected void release() {
        //取消注册
        if (homePresenter != null) {
            homePresenter.unregisterViewCallBack(this);
        }
    }

    @Override
    protected void onRetryClick() {
       //网络错误被点击了
        //重新加载分类
       // LogUtils.d(this,"onRetryClick");
        if (homePresenter != null) {
            homePresenter.getCattgories();
        }
    }
}
